package com.ts.api.module.protocol.service.impl;

import com.gitee.apanlh.util.cache.local.Cache;
import com.gitee.apanlh.util.cache.local.CacheUtils;
import com.ts.api.module.protocol.dao.ProtocolDao;
import com.ts.api.module.protocol.entity.po.ProtocolPO;
import com.ts.api.module.protocol.service.ProtocolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProtocolServiceImpl implements ProtocolService {

    @Autowired
    private ProtocolDao protocolDao;

    static final Cache<String, ProtocolPO> PROTOCAL_TEXT_CACHE = CacheUtils.cache();

    /**
     * 根据渠道类型所有协议
     *
     * @param type 渠道类型
     * @return List
     */
    @Override
    @Transactional(readOnly = true)
    public List<ProtocolPO> listProtocolByType(Integer type) {
        return protocolDao.listProtocolByType(type);
    }

    @Override
    @Transactional(readOnly = true)
    public List<ProtocolPO> listProtocolNameByUniqueCode(List<String> listUniqueCode) {
        return protocolDao.listProtocolNameByUniqueCode(listUniqueCode);
    }

    @Override
    @Transactional(readOnly = true)
    public ProtocolPO getProtocolText(String protocolUniqueCode) {
        return PROTOCAL_TEXT_CACHE.get(protocolUniqueCode, () -> protocolDao.getProtocolText(protocolUniqueCode));
//        return protocolDao.getProtocolText(protocolUniqueCode);
    }

}
